/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
// 二叉搜索树左子树都比当前节点小  右子树都比当前节点大
// 平衡二叉搜索树的话 每次构建节点取区间中间即可
var sortedArrayToBST = function(nums) {
  const dfs = (left, right) => {
    if(left > right) return null
    let mid = (left + right) >> 1
    let tnode = new TreeNode(nums[mid])
    tnode.left = dfs(left, mid - 1)
    tnode.right = dfs(mid + 1, right)
    return tnode
  }
  return dfs(0, nums.length - 1)
};